package com.winit.openapi.wop.inbound.builder;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.alibaba.fastjson.JSONObject;
import com.winit.oms.spi.inbound.vo.DirectInnerInboundOrderVo;
import com.winit.oms.spi.inbound.vo.DirectWhInboundOrderVo;
import com.winit.oms.spi.inbound.vo.InboundDispatchinfoVo;
import com.winit.oms.spi.inbound.vo.InboundEnumConstant;
import com.winit.oms.spi.inbound.vo.InboundMerchandiseDetailsVo;
import com.winit.oms.spi.inbound.vo.InboundOrderTrackingVo;
import com.winit.oms.spi.inbound.vo.InboundOrderVo;
import com.winit.oms.spi.inbound.vo.InboundPackageVo;
import com.winit.oms.spi.inbound.vo.StandardInboundOrderVo;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.util.DateUtil;
import com.winit.openapi.wop.inbound.constant.InboundParameter.InputInboundCreate;
import com.winit.pms.spi.winit.vo.WinitProduct;

/**
 * 海外仓入库单构造器
 * 
 * @version <pre>
 * Author     Version       Date        Changes
 * dinghua.an   1.0         2015年11月10日     Created
 *
 * </pre>
 * @since 1.
 */
public class InboundOrderBuilder {
	private final static String EXPORT_DECLARATION_TYPE_SELF = "SelfDeclaration" ;

    /**
     * 构建订单列表页面的行值
     * 
     * @param json
     * @return
     */
    public static Map<String, Object> buildInboundOrderListPageVo(InboundOrderVo inboundOrder) {
        Map<String, Object> map = new HashMap<String, Object>();

        map.put("slaCalType", inboundOrder.getSlaCalType());
        map.put("isOnTime", inboundOrder.getIsOnTime());
        map.put("serviceStatus", inboundOrder.getServiceStatus());
        map.put("serviceStandardTime", inboundOrder.getServiceStandardTime());
        map.put("serviceCompleteTime", inboundOrder.getServiceCompleteTime());
        map.put("entryWhType", inboundOrder.getEntryWhType());
        map.put("orderNo", inboundOrder.getOrderNo());
        map.put("needReservationSendWh", inboundOrder.getNeedReservationSendWh());
        map.put("destWhName", inboundOrder.getDestWhName());
        map.put("winitProductName", inboundOrder.getWinitProductName());
        map.put("shelveCompletedDate", inboundOrder.getShelveCompletedDate());
        map.put("isFinance", inboundOrder.getIsFinance());
        map.put("dispatchPrintable", inboundOrder.getDispatchPrintable());

        map.put("mergeType", inboundOrder.getMergeType());
        map.put("mainOrderNo", inboundOrder.getMainOrderNo());

        map.put("pickupType", inboundOrder.getPickupType());
        map.put("inspectionWhName", inboundOrder.getInspectionWhName());
        String pickupCompletedDate = DateUtil.parse2String(inboundOrder.getPickupCompletedDate(),
            DateUtil.FULL_DATE_STRING);
        map.put("pickupCompletedDate", pickupCompletedDate);

        map.put("packageQty",
            (inboundOrder.getOrderPackageQty() == null ? 0 : inboundOrder.getOrderPackageQty()) + "|"
                    + (inboundOrder.getActualOrderPackageQty() == null ? 0 : inboundOrder.getActualOrderPackageQty()));

        map.put("itemQty", (inboundOrder.getOrderItemQty() == null ? 0 : inboundOrder.getOrderItemQty()) + "|"
                           + (inboundOrder.getActualOrderItemQty() == null ? 0 : inboundOrder.getActualOrderItemQty()));

        BigDecimal weight = inboundOrder.getOrderWeight();
        if (weight == null || BigDecimal.ZERO.compareTo(weight) >= 0) {
            weight = BigDecimal.ZERO;
        }
        BigDecimal acthalWeight = inboundOrder.getActualOrderWeight();
        if (acthalWeight == null || BigDecimal.ZERO.compareTo(acthalWeight) >= 0) {
            acthalWeight = BigDecimal.ZERO;
        }
        map.put("weight", weight + "|" + acthalWeight);

        BigDecimal volumn = inboundOrder.getOrderVolume();
        if (volumn == null || BigDecimal.ZERO.compareTo(volumn) >= 0) {
            volumn = BigDecimal.ZERO;
        }
        BigDecimal acthalVolumn = inboundOrder.getActualOrderVolume();
        if (acthalVolumn == null || BigDecimal.ZERO.compareTo(acthalVolumn) >= 0) {
            acthalVolumn = BigDecimal.ZERO;
        }
        map.put("volumn", volumn + "|" + acthalVolumn);

        String orderDate = DateUtil.parse2String(inboundOrder.getOrderDate(), DateUtil.FULL_DATE_STRING);
        map.put("orderDate", orderDate);
        String voidDate = DateUtil.parse2String(inboundOrder.getVoidDate(), DateUtil.FULL_DATE_STRING);
        map.put("voidDate", voidDate);

        map.put("status", inboundOrder.getStatus());
        map.put("reservationOrderNo", inboundOrder.getReservationOrderNo());
        map.put("takeEffectDate", inboundOrder.getTakeEffectDate());
        InboundEnumConstant.OrderStatus status = InboundEnumConstant.OrderStatus.valueOf(inboundOrder.getStatus());
        if (status != null) {
            map.put("statusDesc", status.getDesc());
        } else {
            map.put("statusDesc", "undefined");
        }

        return map;
    }

    /**
     * 构建基本信息
     * 
     * @param json
     * @return
     */
    public static InboundOrderVo buildBasicInfo(JSONObject json,String onwardVoyageType,boolean isSelfInspection) {
        String orderType = json.getString("orderType").trim();
        if (InboundEnumConstant.OrderType.STANDARD.getType().equals(orderType)) {
            StandardInboundOrderVo vo = new StandardInboundOrderVo();
            buildCommon(vo, json);
            if (json.containsKey("isFinance")) {
                vo.setIsFinance(json.getString("isFinance").trim());
                if ("Y".equals(json.getString("isFinance"))) {
                    // 贷款单号
                    if (json.containsKey("loanNo")) {
                        vo.setLoanNo(json.getString("loanNo").trim());
                    }
                }
            }
            return vo;
        } else if (InboundEnumConstant.OrderType.DIRECT_INNER.getType().equals(orderType)) {
            DirectInnerInboundOrderVo vo = new DirectInnerInboundOrderVo();
            buildCommon(vo, json);
            return vo;
        } else if (InboundEnumConstant.OrderType.DIRECT_WH.getType().equals(orderType)) {
            DirectWhInboundOrderVo vo = new DirectWhInboundOrderVo();
            buildCommon(vo, json);
            if("Direct".equals(onwardVoyageType)){
            	vo.setInspectionWhCode(vo.getDestWhCode());
            }
            return vo;
        } else if (InboundEnumConstant.OrderType.LOCATION_INBOUND.getType().equals(orderType)) {
            DirectWhInboundOrderVo vo = new DirectWhInboundOrderVo();
            buildCommon(vo, json);
            return vo;
        }else if (InboundEnumConstant.OrderType.STORAGE_INBOUND.getType().equals(orderType)) {
            StandardInboundOrderVo vo = new StandardInboundOrderVo();
            buildCommon(vo, json);
            if (json.containsKey("isFinance")) {
                vo.setIsFinance(json.getString("isFinance").trim());
                if ("Y".equals(json.getString("isFinance"))) {
                    // 贷款单号
                    if (json.containsKey("loanNo")) {
                        vo.setLoanNo(json.getString("loanNo").trim());
                    }
                }
            }
            return vo;
        }else if (InboundEnumConstant.OrderType.STORAGE_DIRECT_INNER.getType().equals(orderType)) {
            DirectInnerInboundOrderVo vo = new DirectInnerInboundOrderVo();
            buildCommon(vo, json);
            return vo;
        }else {
            throw new ApiException("订单类型不支持!");
        }
    }

    /**
     * 构建提货信息
     * 
     * @param json
     * @param vo
     */
    public static void buildPickupInfo(JSONObject json, InboundOrderVo vo) {
        JSONObject dispatchInfoJson = json.getJSONObject("dispatchInfo");
        if (InboundEnumConstant.OrderType.STANDARD.getType().equals(vo.getEntryWhType()) || InboundEnumConstant.OrderType.STORAGE_INBOUND.getType().equals(vo.getEntryWhType())) {
            InboundDispatchinfoVo dispatchInfo = InboundPickupBuilder.buildInspectionPickUp(dispatchInfoJson);
            vo.setInboundDispatchinfoVo(dispatchInfo);
            vo.setInspectionType(dispatchInfo.getInspectionType());
            vo.setPickupType(dispatchInfo.getPickupType());
        } else if (InboundEnumConstant.OrderType.DIRECT_INNER.getType().equals(vo.getEntryWhType())) {
        	if(null != dispatchInfoJson){
        		InboundDispatchinfoVo dispatchInfo = InboundPickupBuilder.buildInspectionPickUp(dispatchInfoJson);
        		vo.setInspectionType(dispatchInfo.getInspectionType());
        		vo.setPickupType(dispatchInfo.getPickupType());
        		vo.setInboundDispatchinfoVo(dispatchInfo);
        	}
        } else if (InboundEnumConstant.OrderType.DIRECT_WH.getType().equals(vo.getEntryWhType())
        		||InboundEnumConstant.OrderType.STORAGE_DIRECT_INNER.getType().equals(vo.getEntryWhType())) {
            // 直发海外无物流计划和提货/验货信息
        } else if (InboundEnumConstant.OrderType.LOCATION_INBOUND.getType().equals(vo.getEntryWhType())) {
            InboundDispatchinfoVo dispatchInfo = InboundPickupBuilder.buildInspectionPickUp(dispatchInfoJson);
            vo.setInspectionType(dispatchInfo.getInspectionType());
            vo.setPickupType(dispatchInfo.getPickupType());
            vo.setInboundDispatchinfoVo(dispatchInfo);
        } else {
            throw new ApiException("订单类型不支持！");
        }
    }

    /**
     * 新建vo的方式build提货信息
     * 
     * @param json
     * @param vo
     */
    public static InboundOrderVo buildUnpickupInboundOrder(JSONObject json) {
        String entryWhType = (String) json.get("entryWhType");
        InboundOrderVo result = new InboundOrderVo();
        result.setEntryWhType(entryWhType);
        buildLogisticInfo(json, result);
        return result;
    }

    /**
     * 组装查询订单详情返回参数
     * 
     * @param order
     * @param orderType
     * @return
     */
    public static Map<String, Object> buildReturnInboundOrderInfo(InboundOrderVo order) {
        Map<String, Object> map = new HashMap<String, Object>();
        /************************** 基本信息 ***********************/
        map.put("orderNo", order.getOrderNo());
        map.put("needReservationSendWh", order.getNeedReservationSendWh());
        map.put("customerOrderNo", order.getCustomerOrderNo());
        map.put("warehouseCode", order.getInspectionWhCode());
        map.put("warehouseName", order.getInspectionWhName());
        map.put("destWhCode", order.getDestWhCode());
        map.put("destWhName", order.getDestWhName());
        map.put("winitProductCode", order.getWinitProductCode());
        map.put("winitProductName", order.getWinitProductName());
        map.put("desc", order.getDesc());
        map.put("status", order.getStatus());
        map.put("createdDate", (DateUtil.parse2String(order.getCreated(), DateUtil.FULL_DATE_STRING)));
        map.put("logisticsPlanNo", order.getLogisticsPlanId());
        map.put("logisticsPlanName", order.getLogisticsPlanName());
        map.put("importerCode", order.getImporterId());
        map.put("importerName", order.getImporterName());
        map.put("exporterCode", order.getExporterId());
        map.put("exporterName", order.getExporterName());
        map.put("orderPackageQty", order.getOrderPackageQty());
        map.put("orderMerchandiseQty", order.getOrderMerchandiseQty());
        map.put("orderItemQty", order.getOrderItemQty());
        map.put("takeEffectDate", order.getTakeEffectDate());
        map.put("isFinance", order.getIsFinance());
        map.put("loanNo", order.getLoanNo());
        map.put("customsData", order.getCustomsData());
        map.put("estimateShelveCompletedDate", order.getEstimateShelveCompletedDate());
        /************************* 验货提货 ************************/
        if (order.getInboundDispatchinfoVo() == null) {
            order.setInboundDispatchinfoVo(new InboundDispatchinfoVo());
        }
        map.put("inspectionType", order.getInboundDispatchinfoVo().getInspectionType());
        map.put("pickupType", order.getInboundDispatchinfoVo().getPickupType());
        map.put("dateExpectFrom", order.getInboundDispatchinfoVo().getExpectFromDate());
        map.put("dateExpectTo", order.getInboundDispatchinfoVo().getExpectToDate());
        map.put("addressCode", order.getInboundDispatchinfoVo().getAddressCode());
        map.put("address", order.getInboundDispatchinfoVo().getAddress());
        map.put("expressVendorCode", order.getInboundDispatchinfoVo().getExpressVendorCode());
        map.put("expressVendorName", order.getInboundDispatchinfoVo().getExpressVendorName());
        map.put("expressNo", order.getInboundDispatchinfoVo().getExpressNo());
        map.put("dispatchPrintable", order.getInboundDispatchinfoVo().getDispatchPrintable());
        buildDateAndTime(map, order.getInboundDispatchinfoVo().getExpectFromDate(), order.getInboundDispatchinfoVo()
            .getExpectToDate());
        return map;

    }

    /**
     * 组装查询订单详情返回参数
     * 
     * @param order
     * @param orderType
     * @return
     */
    public static Map<String, Object> buildReturnInboundOrderInfoForAPI(InboundOrderVo order, boolean isIncludePackage) {
        Map<String, Object> map = new HashMap<String, Object>();
        /************************** 基本信息 ***********************/
        map.put("orderNo", order.getOrderNo());
        map.put("orderType", order.getEntryWhType());
        map.put("status", order.getStatus());
        map.put("needReservationSendWh", order.getNeedReservationSendWh());
        map.put("sellerOrderNo", order.getCustomerOrderNo());
        map.put("inspectionWarehouseCode", order.getInspectionWhCode());
        map.put("inspectionWarehouseName", order.getInspectionWhName());
        map.put("destinationWarehouseCode", order.getDestWhCode());
        map.put("destinationWarehouseName", order.getDestWhName());
        map.put("createdDate", (DateUtil.parse2String(order.getCreated(), DateUtil.FULL_DATE_STRING)));
        //添加上架完成时间
        map.put("shelveCompletedDate", order.getShelveCompletedDate());
        map.put("winitProductCode", order.getWinitProductCode());
        map.put("winitProductName", order.getWinitProductName());
        map.put("planShelfCompletedDate", order.getEstimateShelveCompletedDate());
        map.put("logisticsPlanNo", order.getLogisticsPlanId());
        map.put("logisticsPlanName", order.getLogisticsPlanName());
        map.put("importDeclarationRuleCode", order.getImportDeclarationRuleCode()); //进口报关规则编码
        map.put("importDeclarationRuleName", order.getImportDeclarationRuleName()); //进口报关规则名称
        map.put("importDeclarationType", order.getImportDeclarationType());  //进口报关类型编码
        map.put("importDeclarationName", order.getImportDeclarationName()); //进口报关类型名称
        map.put("customsDeclarationType", order.getCustomsDeclarationType());  //出口报关类型编码
        map.put("customsDeclarationName", order.getCustomsDeclarationName());  //出口报关类型名称
        map.put("importDeclareWay", order.getImportDeclareWay());  //报关方式
        map.put("importDeclareValueType", order.getImportDeclareValueType());  //进口申报价值
        map.put("importerCode", order.getImporterId());
        map.put("importerName", order.getImporterName());
        map.put("exporterCode", order.getExporterId());
        map.put("exporterName", order.getExporterName());
        map.put("totalPackageQty", order.getOrderPackageQty());
        map.put("totalMerchandiseQty", order.getOrderMerchandiseQty());
        map.put("totalItemQty", order.getOrderItemQty());

        /************************* 验货提货 ************************/
        if (order.getInboundDispatchinfoVo() == null) {
            order.setInboundDispatchinfoVo(new InboundDispatchinfoVo());
        }
        map.put("inspectionType", order.getInboundDispatchinfoVo().getInspectionType());
        map.put("pickupType", order.getInboundDispatchinfoVo().getPickupType());
        map.put("pickupAddressCode", order.getInboundDispatchinfoVo().getAddressCode());
        map.put("pickupAddress", order.getInboundDispatchinfoVo().getAddress());
        map.put("expressNo", order.getInboundDispatchinfoVo().getExpressNo());
        map.put("expressVendorCode", order.getInboundDispatchinfoVo().getExpressVendorCode());
        map.put("expressVendorName", order.getInboundDispatchinfoVo().getExpressVendorName());
        if (null != order.getInboundDispatchinfoVo().getExpectFromDate()
            && null != order.getInboundDispatchinfoVo().getExpectToDate()) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
            String dateStr = dateFormat.format(order.getInboundDispatchinfoVo().getExpectFromDate());
            String fromTimeStr = timeFormat.format(order.getInboundDispatchinfoVo().getExpectFromDate());
            String toTimeStr = timeFormat.format(order.getInboundDispatchinfoVo().getExpectToDate());
            map.put("reservePickupDate", dateStr);
            map.put("reservePickupTime", fromTimeStr + "-" + toTimeStr);
        }
        // 包裹信息
        if (isIncludePackage) {
            List<Map<String, Object>> packageList = new ArrayList<Map<String, Object>>();
            List<InboundPackageVo> pkgList = order.getPkgList();
            if (CollectionUtils.isNotEmpty(pkgList)) {
                for (InboundPackageVo inboundPackageVo : pkgList) {
                    Map<String, Object> packMap = new HashMap<String, Object>();
                    packMap.put("sellerCaseNo", inboundPackageVo.getCaseNo());
                    packMap.put("sellerHeight", inboundPackageVo.getSellerHeight());
                    packMap.put("sellerLength", inboundPackageVo.getSellerLength());
                    packMap.put("sellerWeight", inboundPackageVo.getSellerWeight());
                    packMap.put("sellerWidth", inboundPackageVo.getSellerWidth());
                    packMap.put("packageNo", inboundPackageVo.getPackageSerNo());
                    packMap.put("height", inboundPackageVo.getHeight());
                    packMap.put("length", inboundPackageVo.getLength());
                    packMap.put("weight", inboundPackageVo.getWeight());
                    packMap.put("width", inboundPackageVo.getWidth());
                    packageList.add(packMap);

                    //包裹商品信息
                    List<InboundMerchandiseDetailsVo> merchandiseDetailsList = inboundPackageVo.getMerchandiseDetailsList();
                    List<Map<String, Object>> merMapList = new ArrayList<Map<String, Object>>();
                    if (CollectionUtils.isNotEmpty(merchandiseDetailsList)) {
                        for (InboundMerchandiseDetailsVo inboundMerchandiseDetailsVo : merchandiseDetailsList) {
                            Map<String, Object> merMap = new HashMap<String, Object>();
                            merMap.put("sku", inboundMerchandiseDetailsVo.getMerchandiseSerno());
                            merMap.put("merchandiseCode", inboundMerchandiseDetailsVo.getMerchandiseCode());
                            merMap.put("specification", inboundMerchandiseDetailsVo.getSpecification());
							merMap.put("quantity", inboundMerchandiseDetailsVo.getEstimateQty());
							if(InboundEnumConstant.OrderStatus.SHD.getStatus().equals(order.getStatus())){
								merMap.put("actualQuantity", inboundMerchandiseDetailsVo.getShelveQty());
							}else{
								merMap.put("actualQuantity", inboundMerchandiseDetailsVo.getActualQty());
							}
							//添加实际验货或上架数量
                                       
                            merMapList.add(merMap);
                        }
                    }

                    packMap.put("merchandiseList", merMapList);
                }
            }
            map.put("packageList", packageList);
        }

        return map;

    }

    /**
     * 构建入库单vo信息
     * 
     * @param json
     * @param winitProduct 
     * @return
     */
    public static InboundOrderVo build(JSONObject json, WinitProduct winitProduct) {
    	String onwardVoyageType = "Y".equals(winitProduct.getIsContainTransnationalTransportSu()) ? "Winit":"Direct";
    	boolean isSelfInspection = "Y".equals(winitProduct.getIsSelfInspection()) ?  true: false;
        InboundOrderVo vo = buildBasicInfo(json,onwardVoyageType,isSelfInspection);
        buildLogisticsInfo(json, vo);
        buildPickupInfo(json, vo);
        buildSendPortInfo(json,vo);
        if(!"Y".equals(winitProduct.getIsContainTransnationalTransportSu())){
        	buildDirectForecastInfo(json,vo);
        }else{
        	if (InboundEnumConstant.OrderType.DIRECT_INNER.getType().equals(vo.getEntryWhType())
            		|| InboundEnumConstant.OrderType.DIRECT_WH.getType().equals(vo.getEntryWhType())
            		|| InboundEnumConstant.OrderType.STORAGE_DIRECT_INNER.getType().equals(vo.getEntryWhType())) {
        		if(json.containsKey(InputInboundCreate.exportDeclarationType.getName()) 
            			&& EXPORT_DECLARATION_TYPE_SELF.equals(json.getString(InputInboundCreate.exportDeclarationType.getName())) ){
        			vo.setCustomsDeclarationType(EXPORT_DECLARATION_TYPE_SELF);
        		}
            		
            }
        }
        List<InboundPackageVo> packageVoList = InboundPackageBuilder.buildPackage(json);
        vo.setInboundPackageVos(packageVoList);
        return vo;
    }

    private static void buildDirectForecastInfo(JSONObject json,
			InboundOrderVo vo) {
    	 JSONObject directForecastInfoJson = json.getJSONObject("directForecastInfo");
         String orderDateStart = directForecastInfoJson.getString("preparedOffPortDate");
         Date preparedOffPortDate = DateUtil.parse2Date(orderDateStart, DateUtil.SIMPLE_DATE_STRING);
         vo.setPreparedOffPortDate(preparedOffPortDate);
         String orderDateEnd = directForecastInfoJson.getString("preparedArrivePortDate");
         vo.setForecastWarehouseTime(orderDateEnd);
         vo.setTransportAgent(directForecastInfoJson.getString("transportAgent"));
         vo.setPickUpNo(directForecastInfoJson.getString("pickUpNo"));
         vo.setSendWarehouseContactName(directForecastInfoJson.getString("sendWarehouseContactName"));
         vo.setSendWarehouseContactNumber(directForecastInfoJson.getString("sendWarehouseContactNumber"));
         vo.setContainerNo(directForecastInfoJson.getString("containerNo"));
         vo.setSealNo(directForecastInfoJson.getString("sealNo"));
		
	}

	private static void buildSendPortInfo(JSONObject orderJson, InboundOrderVo vo) {
    	if(orderJson.containsKey("sendPortInfo")){
    		JSONObject json = orderJson.getJSONObject("sendPortInfo");
    		
    		if(json.containsKey("sendPortDate")){
    			 String sendPortDateStr = json.getString("sendPortDate");
    	         Date sendPortDate = DateUtil.parse2Date(sendPortDateStr, DateUtil.SIMPLE_DATE_STRING);
    	         vo.setSendPortDate(sendPortDate);
    		}
    		if(json.containsKey("containerNo")){
    			vo.setContainerNo(json.getString("containerNo"));
    		}
    		if(json.containsKey("sendPortType")){
    			vo.setSendPortType(json.getString("sendPortType"));
    		}
    		if(json.containsKey("pickupCartonAddressCode")){
    			vo.setPickupCartonAddrCode(json.getString("pickupCartonAddressCode"));
    		}
    		if(json.containsKey("sealNo")){
    			vo.setSealNo(json.getString("sealNo"));
    		}
    		//构建入参
    		vo.setOrderNo(json.getString("orderNo"));
    		vo.setSendPortType(json.getString("sendPortType"));
    	}
		
	}

	/**
     * 构建物流信息
     * 
     * @param json
     * @param vo
     */
    private static void buildLogisticsInfo(JSONObject json, InboundOrderVo vo) {
            // 进口商编码
            if (json.containsKey("importerCode")) {
                vo.setImporterCode(json.getString("importerCode"));
            }
            // 出口商
            if (json.containsKey("exporterCode")) {
                vo.setExporterCode(json.getString("exporterCode"));
            }
            // 物流计划编号
            if (json.containsKey("logisticsPlanNo")) {
                vo.setLogisticsPlanId(json.getLong("logisticsPlanNo"));
            }
			//进口报关规则
            if (json.containsKey("importDeclarationRuleCode")) {
                vo.setImportDeclarationRuleCode(json.getString("importDeclarationRuleCode"));
            }
            //出口报关类型
            if (json.containsKey(InputInboundCreate.exportDeclarationType.getName())) {
                vo.setCustomsDeclarationType(json.getString(InputInboundCreate.exportDeclarationType.getName()));
            }
    }

    /**
     * 构建物流、提货等信息
     * 
     * @param json
     * @param vo
     */
    private static void buildLogisticInfo(JSONObject json, InboundOrderVo vo) {
        if (InboundEnumConstant.OrderType.STANDARD.getType().equals(vo.getEntryWhType())) {
            if (json.containsKey("logistics")) {
                JSONObject logisticsJson = json.getJSONObject("logistics");
                // 设置物流信息，收货、验货信息
                if (logisticsJson.containsKey("importerCode")) {
                    vo.setImporterId(logisticsJson.getLong("importerCode"));
                }
                if (logisticsJson.containsKey("exporterCode")) {
                    vo.setExporterId(logisticsJson.getLong("exporterCode"));
                }
                if (logisticsJson.containsKey("logisticsPlanNo")) {
                    vo.setLogisticsPlanId(logisticsJson.getLong("logisticsPlanNo"));
                }
               //新增出口报关类型字段
            	if(json.containsKey("exportDeclarationType")){
            		 vo.setCustomsDeclarationType(json.getString("exportDeclarationType"));
            	}
            }

            // 设置物流信息，收货、验货信息
            if (json.containsKey("importerCode")) {
                vo.setImporterId(json.getLong("importerCode"));
            }
            if (json.containsKey("exporterCode")) {
                vo.setExporterId(json.getLong("exporterCode"));
            }
            if (json.containsKey("logisticsPlanNo")) {
                vo.setLogisticsPlanId(json.getLong("logisticsPlanNo"));
            }
            //新增出口报关类型字段
        	if(json.containsKey("exportDeclarationType")){
        		 vo.setCustomsDeclarationType(json.getString("exportDeclarationType"));
        	}

            JSONObject dispatchInfoJson = json.getJSONObject("dispatchInfo");
            InboundDispatchinfoVo dispatchInfo = InboundPickupBuilder.buildInspectionPickUp(dispatchInfoJson);
            vo.setInboundDispatchinfoVo(dispatchInfo);
        } else if (InboundEnumConstant.OrderType.DIRECT_INNER.getType().equals(vo.getEntryWhType())) {
            JSONObject dispatchInfoJson = json.getJSONObject("dispatchInfo");
            InboundDispatchinfoVo dispatchInfo = InboundPickupBuilder.buildInspectionPickUp(dispatchInfoJson);
            vo.setInboundDispatchinfoVo(dispatchInfo);
        } else if (InboundEnumConstant.OrderType.DIRECT_WH.getType().equals(vo.getEntryWhType())) {
            // ignore:do nothing
            // 直发海外无物流计划和提货/验货信息
        } else if (InboundEnumConstant.OrderType.LOCATION_INBOUND.getType().equals(vo.getEntryWhType())) {
            JSONObject dispatchInfoJson = json.getJSONObject("dispatchInfo");
            InboundDispatchinfoVo dispatchInfo = InboundPickupBuilder.buildInspectionPickUp(dispatchInfoJson);
            vo.setInboundDispatchinfoVo(dispatchInfo);
        } else {
            throw new ApiException("订单类型不支持！");
        }

        if (json.containsKey("orderNo")) {
            vo.setOrderNo(json.getString("orderNo").trim());
        }
        if (json.containsKey("desc")) {
            vo.setDesc(json.getString("desc").trim());
        }
        if (json.containsKey("customerOrderNo")) {
            vo.setCustomerOrderNo(json.getString("customerOrderNo").trim());
        }
    }

    private static void buildDateAndTime(Map<String, Object> map, Date expectFromDate, Date expectToDate) {
        if (null == expectFromDate || null == expectToDate) {
            // TODO 如果值为空，则写死数据给前端联调先
            expectFromDate = new Date();
            expectToDate = new Date(System.currentTimeMillis() + 10000);
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
        String dateStr = dateFormat.format(expectFromDate);
        String fromTimeStr = timeFormat.format(expectFromDate);
        String toTimeStr = timeFormat.format(expectToDate);

        map.put("pickExpectDate", dateStr);
        map.put("timeFromTo", fromTimeStr + "-" + toTimeStr);
        // System.out.println(map.get("date") + "\r" + map.get("timeFromTo"));
    }

    private static void buildCommon(InboundOrderVo vo, JSONObject json) {
        // 1、基本信息
        // 验货仓
        if (json.containsKey("inspectionWarehouseCode")) {
            vo.setInspectionWhCode(json.getString("inspectionWarehouseCode").trim());
        }
        // 目的仓
        if (json.containsKey("destinationWarehouseCode")) {
            vo.setDestWhCode(json.getString("destinationWarehouseCode").trim());
        }
        // winit产品编码
        if (json.containsKey("winitProductCode")) {
            vo.setWinitProductCode(json.getString("winitProductCode").trim());
        }
        // 客户订单号
        if (json.containsKey("sellerOrderNo")) {
            vo.setCustomerOrderNo(json.getString("sellerOrderNo").trim());
        }
        // 入库方式
        if (json.containsKey("orderType")) {
            vo.setEntryWhType(json.getString("orderType").trim());
        }
        // if (json.containsKey("orderNo")) {
        // vo.setOrderNo(json.getString("orderNo").trim());
        // }

    }

    /**
     * 构建订单轨迹
     * 
     * @return
     */
    public static List<Map<String, Object>> buildInboundOrderTrackingList(List<InboundOrderTrackingVo> tList) {
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        for (InboundOrderTrackingVo vo : tList) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("date", vo.getTrackingDate());
            map.put("trackingDesc", vo.getTrackingDesc());
            map.put("location", vo.getTrackingLocation());
            map.put("trackingCode", vo.getTrackingCode());
            mapList.add(map);
        }

        return mapList;

    }

    public static void main(String[] args) {
        buildDateAndTime(new HashMap<String, Object>(), null, null);
    }
}
